package com.ethen.liveeventbus.service;

import android.life.livedata.Observer;
import com.ethen.liveeventbus.ResourceTable;
import com.ethen.liveeventbus.slice.LiveEventAbilitySlice;
import com.ohos.liveeventbus.LiveEventBus;
import com.ohos.liveeventbus.logger.Log;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

public class IpcService extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "IpcService::onStart");
        super.onStart(intent);

        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST, String.class)
                .observeForever(observer);
        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST_IN_APP, String.class)
                .observeForever(observer);
        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST_GLOBAL, String.class)
                .observeForever(observer);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "IpcService::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "IpcService::onStop");
        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST, String.class)
                .removeObserver(observer);
        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST_IN_APP, String.class)
                .removeObserver(observer);
        LiveEventBus
                .get(LiveEventAbilitySlice.KEY_TEST_BROADCAST_GLOBAL, String.class)
                .removeObserver(observer);
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }

    private final Observer<String> observer = this::showDialog;

    public void showDialog(String s) {
        Log.d("ipcService show :"+ s);
        DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_toast_dialog, null, false);
        Text text = (Text) layout.findComponentById(ResourceTable.Id_txt_dialog);
        text.setText(s);
        new ToastDialog(IpcService.this)
                .setContentCustomComponent(layout)
                .setAlignment(LayoutAlignment.CENTER)
                .setDuration(3000)
                .show();
    }
}